package com.handinfo.android.uicontrols.controls;

import java.util.concurrent.CopyOnWriteArrayList;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.utils.Tools;

/**
 * 伸缩盒子
 * @author handinfo
 */
public class DWStretchBox extends DWControl
{

	private int m_tag_direct = 0;//伸缩提示标签的方向,TOP,BOTTOM,LEFT,RIGHT
	private int m_tag_length = 0;//伸缩距离
	public static boolean TAG_STATE_STRETCH = false;//伸展
	public static boolean TAG_STATE_STRICTION = true;//收缩
	private boolean m_tag_state = TAG_STATE_STRETCH;
	private int m_cur_length = 0;//当前的伸缩距离
	private float m_tag_scale = 3.0f;//伸缩比例,1/3
	private int m_tolerance = 5;
	
	private long m_failure_time = 0;//失效时间
	private long m_failure_start = 0;//失效时间开始的时间
	
	public CopyOnWriteArrayList<DWControl> m_controls = new CopyOnWriteArrayList<DWControl>();

	/**
	 * 创建一个伸缩盒子
	 * @param tag_dir 伸缩方向
	 * @param tag_len 伸缩距离
	 */
	public DWStretchBox(int tag_dir, int tag_len)
	{
		m_tag_direct = tag_dir;
		m_tag_length = tag_len;
	}
	public void render(DWGraphics g)
	{
		for (int i = 0; i < m_controls.size(); i++) {
			switch (m_tag_direct) {
				case Tools.TOP:
					m_controls.get(i).renderLimits(g, m_show_x, m_show_y - m_cur_length, m_show_w, m_show_h, 0, 0);
					break;
				case Tools.BOTTOM:
					m_controls.get(i).renderLimits(g, m_show_x, m_show_y + m_cur_length, m_show_w, m_show_h, 0, 0);
					break;
				case Tools.LEFT:
					m_controls.get(i).renderLimits(g, m_show_x - m_cur_length, m_show_y, m_show_w, m_show_h, 0, 0);
					break;
				case Tools.RIGHT:
					m_controls.get(i).renderLimits(g, m_show_x + m_cur_length, m_show_y, m_show_w, m_show_h, 0, 0);
					break;
				case Tools.LEFT_TOP:
					m_controls.get(i).renderLimits(g, m_show_x - m_cur_length, m_show_y - m_cur_length, m_show_w, m_show_h, 0, 0);
					break;
				case Tools.RIGHT_TOP:
					m_controls.get(i).renderLimits(g, m_show_x + m_cur_length, m_show_y - m_cur_length, m_show_w, m_show_h, 0, 0);
					break;
				case Tools.RIGHT_BOTTOM:
					m_controls.get(i).renderLimits(g, m_show_x + m_cur_length, m_show_y + m_cur_length, m_show_w, m_show_h, 0, 0);
					break;
				case Tools.LEFT_BOTTOM:
					m_controls.get(i).renderLimits(g, m_show_x - m_cur_length, m_show_y + m_cur_length, m_show_w, m_show_h, 0, 0);
					break;
			} 
		}
	}
	/**
	 * 设置伸缩状态
	 * @param state
	 */
	public void setState(boolean state) {
		m_tag_state = state;
		m_failure_start = 0;
	}
	
	/**
	 * 设置伸缩比例
	 * @param scale
	 */
	public void setScale(float scale) {
		m_tag_scale = scale;
	}
	
	/**
	 * 设置自动收回时间
	 * @param sceond 秒
	 */
	public void setStretchTime(long sceond) {
		m_failure_time = sceond * 1000;
	}

	public void logic()
	{
		if (m_tag_state == TAG_STATE_STRICTION) {//收缩
			if (m_cur_length < m_tag_length) {
				m_cur_length += ((m_tag_length - m_cur_length) / m_tag_scale);
				if (m_tag_length - m_cur_length < m_tolerance) {
					m_cur_length = m_tag_length;
				}
			} else {
				m_cur_length = m_tag_length;
			}
		} else if (m_tag_state == TAG_STATE_STRETCH) {//伸展
			if (m_cur_length > 0) {
				m_cur_length -= (m_cur_length / m_tag_scale);
				if (m_cur_length < m_tolerance) {
					m_cur_length = 0;
				}
			} else {
				m_cur_length = 0;
			}
		}
		if (m_tag_state == TAG_STATE_STRETCH && m_failure_time > 0) {
			long currentTime = System.currentTimeMillis();
			if (m_failure_start == 0) {
				m_failure_start = currentTime;
			}
			if (currentTime - m_failure_start >= m_failure_time) {
				DWControl control = DWControlsManager.getInstance().m_guide_control;
				if (control != null) {
					CopyOnWriteArrayList<DWControl> list = m_controls;
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i) == control) {
							return;
						}
					}
				}
				setState(TAG_STATE_STRICTION);
			}
		}
	}
	public void refreshControl()
	{
		DWControl control = null;
		int maxW = 0;
		int maxH = 0;
		for (int i = 0, w = 0, h = 0; i < m_controls.size(); i++) {
			control = m_controls.get(i);
			if (control != null) {
				control.setDefaultNearAnchorUn();
				if (i == 0) {
					control.setPointUn(control.m_show_x - m_show_x, control.m_show_y - m_show_y);
				}
			}
			w = control.m_show_x + control.m_show_w;
			maxW = maxW < w ? w : maxW;
			h = control.m_show_y + control.m_show_h;
			maxH = maxH < h ? h : maxH;
		}
		setShowWideHighUn(maxW, maxH);
	}

	public void addControl(DWControl control)
	{
		if (control != null) {
			control.setRelyControl(this);
			m_controls.add(control);
			refreshControl();
		}
	}

	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y) && m_tag_state == TAG_STATE_STRETCH) {
			for (int i = 0; i < m_controls.size(); i++) {
				if (m_controls.get(i).doClick(x, y)) {
					return true;
				}
			}
		}
		return false;
	}

}
